
# data uitls
pacman::p_load(data.table, tidyverse, rio)
#  panel / factor model packages
pacman::p_load(synthdid)
set.seed(42)
if (Sys.info()[['user']] == "alal") {
  root = "/home/alal/Dropbox/1_Research/ElecAdminFunding/ElecAdminFundingReplication"
} else {
  root = "~/Dropbox/ElecAdminFunding/ElecAdminFundingReplication"
}

# Set up the analysis data
raw = rio::import(file.path(root, "/modified_data/ctcl.dta")) %>% setDT
raw[, treated := ifelse((amount > 0) & policy_year == 2020, 1, 0)]
raw[policy_year == 2020, .N, treated]
raw[, ever_treated := max(treated), fips]


##
# Synth diff-in-diff estimates by expenditure tercile
##

# Estimate synth diff-in-diff for effect on Dem vote share
# for first expenditure tercile
t1_synth_dem = panel_estimate(filter(raw, amt_tercile==0 | amt_tercile==1),
    "fips", "policy_year", "treated", "vs_dem_pres",
    methods=c("Synthetic DID (SDID)"), mccores = 14, reps = 1000,
  infmethod = "bootstrap")

# Estimate synth diff-in-diff for effect on Dem vote share
# for second expenditure tercile
t2_synth_dem = panel_estimate(filter(raw, amt_tercile==0 | amt_tercile==2),
    "fips", "policy_year", "treated", "vs_dem_pres",
    methods=c("Synthetic DID (SDID)"), mccores = 14, reps = 1000,
  infmethod = "bootstrap")

# Estimate synth diff-in-diff for effect on Dem vote share
# for third expenditure tercile
t3_synth_dem = panel_estimate(filter(raw, amt_tercile==0 | amt_tercile==3),
    "fips", "policy_year", "treated", "vs_dem_pres",
    methods=c("Synthetic DID (SDID)"), mccores = 14, reps = 1000,
  infmethod = "bootstrap")

# Estimate synth diff-in-diff for effect on turnout
# for first expenditure tercile
t1_synth_tur = panel_estimate(filter(raw, amt_tercile==0 | amt_tercile==1),
    "fips", "policy_year", "treated", "turnout_pres",
    methods=c("Synthetic DID (SDID)"), mccores = 14, reps = 1000,
  infmethod = "bootstrap")

# Estimate synth diff-in-diff for effect on turnout
# for second expenditure tercile
t2_synth_tur = panel_estimate(filter(raw, amt_tercile==0 | amt_tercile==2),
    "fips", "policy_year", "treated", "turnout_pres",
    methods=c("Synthetic DID (SDID)"), mccores = 14, reps = 1000,
  infmethod = "bootstrap")

# Estimate synth diff-in-diff for effect on turnout
# for third expenditure tercile
t3_synth_tur = panel_estimate(filter(raw, amt_tercile==0 | amt_tercile==3),
    "fips", "policy_year", "treated", "turnout_pres",
    methods=c("Synthetic DID (SDID)"), mccores = 14, reps = 1000,
  infmethod = "bootstrap")

save(
  t1_synth_dem, t1_synth_tur,
  t2_synth_dem, t2_synth_tur,
  t3_synth_dem, t3_synth_tur,
  file = file.path(root, "tmp/synth_exp_terciles.RData")
)


##
# Synth diff-in-diff estimates by VAP tercile
##

# Estimate synth diff-in-diff for effect on Dem vote share
# for first population tercile
vap_t1_synth_dem = panel_estimate(filter(raw, vap_tercile==1),
    "fips", "policy_year", "treated", "vs_dem_pres",
    methods=c("Synthetic DID (SDID)"), mccores = 14, reps = 1000,
  infmethod = "bootstrap")

# Estimate synth diff-in-diff for effect on Dem vote share
# for second population tercile
vap_t2_synth_dem = panel_estimate(filter(raw, vap_tercile==2),
    "fips", "policy_year", "treated", "vs_dem_pres",
    methods=c("Synthetic DID (SDID)"), mccores = 14, reps = 1000,
  infmethod = "bootstrap")

# Estimate synth diff-in-diff for effect on Dem vote share
# for third population tercile
vap_t3_synth_dem = panel_estimate(filter(raw, vap_tercile==3),
    "fips", "policy_year", "treated", "vs_dem_pres",
    methods=c("Synthetic DID (SDID)"), mccores = 14, reps = 1000,
  infmethod = "bootstrap")

# Estimate synth diff-in-diff for effect on turnout
# for first population tercile
vap_t1_synth_tur = panel_estimate(filter(raw, vap_tercile==1),
    "fips", "policy_year", "treated", "turnout_pres",
    methods=c("Synthetic DID (SDID)"), mccores = 14, reps = 1000,
  infmethod = "bootstrap")

# Estimate synth diff-in-diff for effect on turnout
# for second population tercile
vap_t2_synth_tur = panel_estimate(filter(raw, vap_tercile==2),
    "fips", "policy_year", "treated", "turnout_pres",
    methods=c("Synthetic DID (SDID)"), mccores = 14, reps = 1000,
  infmethod = "bootstrap")

# Estimate synth diff-in-diff for effect on turnout
# for third population tercile
vap_t3_synth_tur = panel_estimate(filter(raw, vap_tercile==3),
    "fips", "policy_year", "treated", "turnout_pres",
    methods=c("Synthetic DID (SDID)"), mccores = 14, reps = 1000,
  infmethod = "bootstrap")

save(
  vap_t1_synth_dem, vap_t1_synth_tur,
  vap_t2_synth_dem, vap_t2_synth_tur,
  vap_t3_synth_dem, vap_t3_synth_tur,
  file = file.path(root, "tmp/synth_vapTerciles.RData")
)


##
# Synth diff-in-diff estimates by
##

battleground_narrow = c("FL", "GA", "NC", "AZ", "PA", "NV")
battleground_broad = c("AZ", "CO", "FL", "GA", "IA", "NC", "NV", "OH", "PA", "TX")

# Estimate synth diff-in-diff for effect on Dem vote share
# for narrow battleground definition (less than 5pp)
battleground_narrow_synth_dem = panel_estimate(filter(raw, state %in% battleground_narrow),
    "fips", "policy_year", "treated", "vs_dem_pres",
    methods=c("Synthetic DID (SDID)"), mccores = 14, reps = 1000,
  infmethod = "bootstrap")

# Estimate synth diff-in-diff for effect on Dem vote share
# for narrow battleground definition (Cook)
battleground_broad_synth_dem = panel_estimate(filter(raw, state %in% battleground_broad),
    "fips", "policy_year", "treated", "vs_dem_pres",
    methods=c("Synthetic DID (SDID)"), mccores = 14, reps = 1000,
  infmethod = "bootstrap")

# Estimate synth diff-in-diff for effect on turnout
# for narrow battleground definition (less than 5pp)
battleground_narrow_synth_tur = panel_estimate(filter(raw, state %in% battleground_narrow),
    "fips", "policy_year", "treated", "turnout_pres",
    methods=c("Synthetic DID (SDID)"), mccores = 14, reps = 1000,
  infmethod = "bootstrap")

# Estimate synth diff-in-diff for effect on turnout
# for narrow battleground definition (Cook)
battleground_broad_synth_tur = panel_estimate(filter(raw, state %in% battleground_broad),
    "fips", "policy_year", "treated", "turnout_pres",
    methods=c("Synthetic DID (SDID)"), mccores = 14, reps = 1000,
  infmethod = "bootstrap")

save(
  battleground_narrow_synth_dem,
  battleground_broad_synth_dem,
  battleground_narrow_synth_tur,
  battleground_broad_synth_tur,
  file = file.path(root, "tmp/synth_battleground.RData")
)

